#include <iostream>
#include "BinaryTree.h"

using namespace std;

// 90.75 %
class Solution
{
  public:
    TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2)
    {
        if (!t1 && !t2)
            return NULL;
        TreeNode *res = new TreeNode((t1 ? t1->val : 0) + (t2 ? t2->val : 0));
        res->left = mergeTrees(t1 ? t1->left : NULL, t2 ? t2->left : NULL);
        res->right = mergeTrees(t1 ? t1->right : NULL, t2 ? t2->right : NULL);
        return res;
    }
};

int main()
{
    int arr1[] = {1, 3, 5, 0, 0, 0, 2, 0, 0};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[] = {2, 1, 0, 4, 0, 0, 3, 0, 7, 0, 0};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    TreeNode *tree1 = createBinaryTree(arr1, n1);
    TreeNode *tree2 = createBinaryTree(arr2, n2);
    preorderPrint(tree1);
    cout << endl;
    preorderPrint(tree2);
    cout << endl;
    TreeNode *res = Solution().mergeTrees(tree1, tree2);
    preorderPrint(res);
    return 0;
}
